package com.yang.service;

import com.yang.entity.Ads;
import com.yang.dao.AdsDao;
import com.yang.util.RespEntity;
import sun.security.provider.Sun;

import java.util.*;

public class AdsService {
    private final AdsDao adsDao = new AdsDao();

    //查询全部
    public List<Ads> queryAds() {
        return adsDao.queryAds();
    }

    //单个查询
    public Ads queryAdsById(Integer id) {
        return adsDao.queryAdsById(id);
    }

    //添加
    public int saveAds(Ads obj) {
        return adsDao.saveAds(obj);
    }

    //删除
    public int deleteAdsById(Integer id) {
        return adsDao.deleteAdsById(id);
    }

    //修改
    public int updateAdsById(Ads obj) {
        return adsDao.updateAdsById(obj);
    }
    //后台广告查询全部
    public RespEntity HtmlqueryAds(){
        List<Ads> ads = adsDao.QuseryAdss();
        return new RespEntity(200,"查询全部广告",ads);
    }
    //后台添加
    public  RespEntity HtmlInsertAds(Ads ads){
        int sum = adsDao.saveAds(ads);
        return new RespEntity(200,"添加", sum);
    }
    //后台单个查询
    public  RespEntity HtmlByIdAds(int id){
        Ads ads = adsDao.queryAdsById(id);
        System.out.println("广告单个查询"+ads.toString());
        return new RespEntity(200,"单个查询",ads);
    }
    //后台修改广告
    public RespEntity HtmlUpdateAds(Ads ads){
        StringBuffer sb = new StringBuffer("update ads set ");
        //判断不为空的拼接sql条件
        if (ads.getTitle()!=null&&!"".equals(ads.getTitle())) {
            sb.append(" title='"+ads.getTitle()+"',");
        }
        if (ads.getImg()!=null&&!"".equals(ads.getImg())) {
            sb.append(" img='"+ads.getImg()+"',");
        }
        if (ads.getRemark()!=null&&!"".equals(ads.getRemark())) {
            sb.append(" remark='"+ads.getRemark()+"',");
        }
        if (ads.getUrl()!=null&&!"".equals(ads.getUrl())){
            sb.append(" url='"+ads.getUrl()+"'");
        }
        sb.append(" where id="+ads.getId());
        System.out.println("广告sql语句："+sb.toString());
        int sum = adsDao.updateBySql(sb.toString());
        return new RespEntity(200,"修改",sum);
    }
    //后台删除
    public RespEntity HtmlDeleteById(int id){
        int sum = adsDao.deleteAdsById(id);
        return  new RespEntity(200,"删除",sum);
    }
}
